package com.zxy.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zxy.annotation.RequestMapping;
import com.zxy.annotation.RequestMappingHandler;
import com.zxy.dao.ZhihuDao;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * @author zxy
 * @date 2019-04-28 15:49
 */
@WebServlet("/zhihu/*")
public class ZhihuController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        String requestURI = request.getRequestURI();
        String action = requestURI.substring(request.getServletContext().getContextPath().length() + 1);
        action = action.substring(action.indexOf("/"));
        RequestMappingHandler.handle(this.getClass(), this, action, request, response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        doPost(req, resp);
    }

    @RequestMapping("/collection")
    public void javaDateFormat(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        ZhihuDao zhihuDao = new ZhihuDao();
        List<Map<String, Object>> mapList = zhihuDao.collection();
        Gson gson = new Gson();
        String json = gson.toJson(mapList);
        out.write(json);
    }

    @RequestMapping("/followingQuestions")
    public void followingQuestions(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        ZhihuDao zhihuDao = new ZhihuDao();
        List<Map<String, Object>> followingQuestions = zhihuDao.followingQuestions();
        Gson gson = new Gson();
        String json = gson.toJson(followingQuestions);
        out.write(json);
    }

    @RequestMapping("/hignAppearanceProgrammer")
    public void hignAppearanceProgrammer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        ZhihuDao zhihuDao = new ZhihuDao();
        Gson gson = new Gson();
        List<Map<String, Object>> hignValueProgrammer = zhihuDao.hignValueProgrammer();
        out.write("<a target='_blank' href='https://www.zhihu.com/question/37787176'>当一个颜值很高的程序员是怎样一番体验？</a><br>\n" + "\n");
        for (int i = 0; i < hignValueProgrammer.size(); i++) {
            Map<String, Object> map = hignValueProgrammer.get(i);
            System.out.println(map);
            String author_url = (String) map.get("author_url");
            String img_urls = (String) map.get("img_urls");
            List<String> img_urls_list = gson.fromJson(img_urls, new TypeToken<List<String>>() {
            }.getType());
            out.write("<a href='" + author_url + "'>" + author_url + "</a><br>");
            for (String img_url : img_urls_list) {
                out.write("<a href='" + img_url + "'>" + img_url + "</a><br>");
            }
        }
    }
}
